﻿using CBS.APP;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddCoursePlan.xaml 的交互逻辑
    /// </summary>
    public partial class AddCoursePlan : Window
    {
        public AddCoursePlan()
        {
            InitializeComponent();

            BindSemester();
            BindTeacher();
            BindGrade();
            BindClass();
            BindCoure();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CoursePlanViewModel coursePlan = new();
            coursePlan.CoursePlanName = tb_CoursePlan.Text.Trim();
            coursePlan.CoursePrice = string.IsNullOrWhiteSpace(tb_Price.Text.Trim()) ? 0 : decimal.Parse(tb_Price.Text.Trim());
            coursePlan.ClassTimeLong = string.IsNullOrWhiteSpace(tb_Long.Text.Trim()) ? 0 : decimal.Parse(tb_Long.Text.Trim());
            coursePlan.TeacherId = ((TeacherSelectModel)cb_Teacher.SelectedItem).TeacherId;
            coursePlan.SemesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            coursePlan.GradeId = ((GradeViewModel)cb_Grade.SelectedItem).GradeId;
            coursePlan.ClassId = ((ClassViewModel)cb_Class.SelectedItem).ClassId;
            coursePlan.CourseId = ((CourseViewModel)cb_Course.SelectedItem).CourseId;
            coursePlan.Memo = tb_Memo.Text.Trim();
            coursePlan.CourseCount = string.IsNullOrWhiteSpace(tb_CourseCount.Text.Trim()) ? 0 : int.Parse(tb_CourseCount.Text.Trim());
            coursePlan.PlanNum = string.IsNullOrWhiteSpace(tb_PlanNum.Text.Trim()) ? 0 : int.Parse(tb_PlanNum.Text.Trim());

            try
            {
                if (CoursePlanApp.CreateCoursePlan(coursePlan))
                {
                    this.DialogResult = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                cb_Semester.SelectedIndex = 0;
                //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindGrade()
        {
            try
            {
                var list = await Task.Run(() => GradeApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Grade.ItemsSource = list;
                }));
                cb_Grade.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindClass()
        {
            try
            {
                var list = await Task.Run(() => ClassApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Class.ItemsSource = list;
                }));
                cb_Class.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindCoure()
        {
            try
            {
                var list = await Task.Run(() => CourseApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Course.ItemsSource = list;
                }));
                cb_Course.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindTeacher()
        {
            try
            {
                var list = await Task.Run(() => TeacherApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Teacher.ItemsSource = list;
                }));
                cb_Teacher.SelectedIndex = 0;
                //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
